package utils

import (
	"fmt"
	"strconv"
)

// ToString 将任意类型转换为字符串
func ToString(v interface{}) string {
	if v == nil {
		return ""
	}
	return fmt.Sprintf("%v", v)
}

// ToUint 将任意类型转换为uint
func ToUint(v interface{}) uint {
	if v == nil {
		return 0
	}

	switch value := v.(type) {
	case int:
		return uint(value)
	case int8:
		return uint(value)
	case int16:
		return uint(value)
	case int32:
		return uint(value)
	case int64:
		return uint(value)
	case uint:
		return value
	case uint8:
		return uint(value)
	case uint16:
		return uint(value)
	case uint32:
		return uint(value)
	case uint64:
		return uint(value)
	case float32:
		return uint(value)
	case float64:
		return uint(value)
	case string:
		i, _ := strconv.ParseUint(value, 10, 64)
		return uint(i)
	default:
		return 0
	}
}
